home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2924 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.8 KB  |  105 lines

  1. Path: news.wwa.com!rmartin
  2. From: rmartin@oma.com (Robert C. Martin)
  3. Newsgroups: comp.object,comp.lang.c++,comp.ai.alife,sci.comp-aided
  4. Subject: Re: [Q] Growing Objects
  5. Date: 20 Jan 1996 15:34:28 GMT
  6. Organization: Object Mentor
  7. Message-ID: <RMARTIN.96Jan20093428@rcm.oma.com>
  8. References: <30FE8297.41C6@bme.jhu.edu>
  9. NNTP-Posting-Host: rmartin.ip.wwa.com
  10. In-reply-to: Harold Bien's message of Thu, 18 Jan 1996 12:25:43 -0500
  11.  
  12. In article <30FE8297.41C6@bme.jhu.edu> Harold Bien <hbien@bme.jhu.edu> writes:
  13.  
  14.     This "Run" function will call another function indirectly.  In 
  15.    simpler terms, it "opens up a box labelled RunMe" and whatever was in
  16.    the box then gains control, and here's where the exciting part comes.
  17.    The function which is "in the box" _CHANGES_OVER_TIME.  Therefore, the 
  18.    functionality of the object "Cell" changes over time.
  19.  
  20. What you have described is known as a finite state machine.  A finite
  21. state machine reacts to particular events in ways that depend upon the
  22. state of the machine.  In your case, you have only one event: Run; but
  23. you have many states. 
  24.  
  25.     The class Cell has a function Run() which takes no arguments but 
  26.    calls another function through a pointer to a member function.  Then, 
  27.    when that member function runs, it updates a variable 'age' and checks
  28.    to see if it is mature.  If so, then it will change the pointer to point
  29.    to another function.
  30.  
  31. You can use pointers to member functions to control your state
  32. machine.  However I recommend against it.  It may be, after some time,
  33. that you find that you need other events beside 'run'.  For example,
  34. you may wish to tell each cell to "print"; and would like the behavior
  35. of "print" to depend upon the state of the cell.  Other events seem
  36. likely as well:  RespondToLight, RespondToHunger, RespondToAttack,
  37. etc.  Each of these stimuli will very likely depend upon the state.
  38. If you use the member function approach, you will find that every time
  39. the cell changes state, you will have to shuttle around many pointers
  40. to member functions.  The chances of error are high.
  41.  
  42. There is a better way, and one that has been used for many years to
  43. implement finite state machines.
  44.  
  45. class Cell;
  46.  
  47. class CellState
  48. {
  49.   public:
  50.     virtual void Run(Cell*) = 0;
  51. };
  52.  
  53. class Cell
  54. {
  55.   public:
  56.     void Run() {itsState->Run(this);}
  57.   private:
  58.     CellState* itsState;
  59. };
  60.  
  61. class Young : public CellState
  62. {
  63.   public:
  64.     virtual void Run(Cell*);
  65. };
  66.  
  67. class Old : public CellState
  68. {
  69.   public:
  70.     virtual void Run(Cell*);
  71. };
  72.  
  73.  
  74. You ought to be able to see how it works from this explanation.
  75. Instead of having the Cell point to a member to a function, you have
  76. it point to an instance of an abstract class.  Instead of changing the
  77. pointer to the member function when the cell changes state, you change
  78. the instance that is being pointed to.
  79.  
  80. Now you can add new events to the abstract base class at will.  The
  81. code which changes the state of the Cell will not have to manage a
  82. bunch of pointers to members.  It need only manage a single pointer to
  83. a CellState derivative.
  84.  
  85. ------
  86.  
  87. The above uses a design pattern known as 'State'.  You can read about
  88. it in "Design Patterns" by Gamma et. al.  Addison Wesley.   
  89.  
  90. The code above can be automatically generated with a tool that I wrote
  91. many years ago, and which I have been offering as public domain on the
  92. net.  The name of this tool is SMC.  SMC generates the C++ code that
  93. implements a finite state machine.  The C++ code that is generated
  94. uses the state pattern as above.
  95.  
  96. If anybody is interested in receiving this tool, send me email and
  97. i'll email the source code to you.
  98.  
  99. --
  100. Robert Martin       | Design Consulting   | Training courses offered:
  101. Object Mentor Assoc.| rmartin@oma.com     |   OOA/D, C++, Advanced OO
  102. 14619 N. Somerset Cr| Tel: (708) 918-1004 |   Mgt. Overview of OOT
  103. Green Oaks IL 60048 | Fax: (708) 918-1023 | Development Contracts.
  104.  
  105.